package cn.org.eshow.dao.hibernate;

import cn.org.eshow.bean.query.AdditionalOrderQuery;
import cn.org.eshow.common.dao.EnhancedRule;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.AdditionalOrderDao;
import cn.org.eshow.model.AdditionalOrder;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("additionalOrderDao")
public class AdditionalOrderDaoHibernate extends GenericDaoHibernate<AdditionalOrder, Integer>
        implements AdditionalOrderDao {


    public AdditionalOrderDaoHibernate() {
        super(AdditionalOrder.class);
    }

    @Override
    public List<AdditionalOrder> list(AdditionalOrderQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return list(rule);
    }

    @Override
    public Page<AdditionalOrder> search(AdditionalOrderQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        if (query != null) {
            rule.setOffset(query.getOffset());
            rule.setPageSize(query.pagesize);
        }
        return page(rule);
    }

    @Override
    public Long count(AdditionalOrderQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return count(AdditionalOrder.class, rule);
    }

    public EnhancedRule setRule(EnhancedRule rule, AdditionalOrderQuery query) {
        if (query != null) {
            if (query.startAddTime != null) {
                rule.add(Restrictions.ge("addTime", query.startAddTime));
            }
            if (query.endAddTime != null) {
                rule.add(Restrictions.le("addTime", query.endAddTime));
            }
            if (query.startUpdateTime != null) {
                rule.add(Restrictions.ge("updateTime", query.startUpdateTime));
            }
            if (query.endUpdateTime != null) {
                rule.add(Restrictions.le("updateTime", query.endUpdateTime));
            }
            if (StringUtils.isNotEmpty(query.name)) {
                rule.add(Restrictions.like("name", query.name, MatchMode.ANYWHERE));
            }
            if (StringUtils.isNotEmpty(query.no)) {
                rule.add(Restrictions.like("no", query.no, MatchMode.ANYWHERE));
            }
            if (query.minPrice != null) {
                rule.add(Restrictions.ge("price", query.minPrice));
            }
            if (query.maxPrice != null) {
                rule.add(Restrictions.le("price", query.maxPrice));
            }
            if (query.payment != null) {
                rule.add(Restrictions.eq("payment", query.payment));
            }
            if (query.state != null) {
                if(query.state!=100){
                    rule.add(Restrictions.eq("state", query.state));
                }else {
                    rule.add(Restrictions.or(Restrictions.eq("state", 2),Restrictions.and(Restrictions.eq("state", 3),Restrictions.eq("deliveryState",0))));
                }
            }
            if (StringUtils.isNotEmpty(query.callback)) {
                rule.add(Restrictions.eq("callback", query.callback));
            }
            if (query.enabled != null) {
                rule.add(Restrictions.eq("enabled", query.enabled));
            }
            if (query.productOrderId != null) {
                rule.add(Restrictions.eq("productOrder.id", query.productOrderId));
            }
            if (query.userId != null) {
                rule.add(Restrictions.eq("user.id", query.userId));
            }
            if (!StringUtils.isEmpty(query.getOrder())) {
                rule.addOrder(query.getDesc() ? Order.desc(query.getOrder()) : Order.asc(query.getOrder()));
            }
        }
        return rule;
    }

}
